/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.bean.UserBean;
import com.minierp.dao.LoanBillDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.LoanBill;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class LoanBillDaoImpl extends BaseDaoImpl implements LoanBillDao {
	public LoanBill get(String billCode) {
		String sql = "from com.minierp.model.LoanBill as obj where obj.billCode = :billCode and obj.enabled=\'Y\' order by obj.id";
		String[] paramNames = new String[]{"billCode"};
		Object[] values = new Object[]{billCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (LoanBill) list.get(0);
	}

	public List<?> getList(String searchKey, String billType, boolean examined,
			long tsId, UserBean ub, Date allowStart, int startIndex, int num)
			throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.loanDate>=:allowStart";
		}

		String examineFlag = "N";
		String orderByStr = "order by b.loanDate";
		if (examined) {
			examineFlag = "Y";
			orderByStr = "order by b.loanDate desc,b.billCode desc";
		}

		String sql = "select b from com.minierp.model.LoanBill b where b.billType=:billType and b.examined=:examined"
				+ str + " and b.enabled=\'Y\' " + orderByStr;
		Session sess = null;

		List arg16;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("billType", billType);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg16 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg16;
	}

	public int getListCount(String searchKey, String billType,
			boolean examined, long tsId, UserBean ub, Date allowStart)
			throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.loanDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select count(b.id) from com.minierp.model.LoanBill b where b.billType=:billType and b.examined=:examined"
				+ str + " and b.enabled=\'Y\'";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("billType", billType);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			List list = q.list();
			if (!list.isEmpty()) {
				int arg14 = Integer.parseInt(list.get(0).toString());
				return arg14;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List<?> getList(String searchKey, String billType, long tsId,
			UserBean ub, Date allowStart, String orderBy, int startIndex,
			int num) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.loanDate>=:allowStart";
		}

		String sql = "select b from com.minierp.model.LoanBill b where b.billType=:billType and b.enabled=\'Y\'"
				+ str;
		if (orderBy != null && orderBy.equals("PAY_DATE_ASC")) {
			sql = sql + " order by b.loanDate asc,b.billCode asc";
		} else {
			sql = sql + " order by b.loanDate desc,b.billCode desc";
		}

		Session sess = null;

		List arg14;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("billType", billType);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg14 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg14;
	}

	public int getListCount(String searchKey, String billType, long tsId,
			UserBean ub, Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.loanDate>=:allowStart";
		}

		String sql = "select count(b.id) from com.minierp.model.LoanBill b where b.billType=:billType and b.enabled=\'Y\'"
				+ str;
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("billType", billType);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (!list.isEmpty()) {
				int arg12 = Integer.parseInt(list.get(0).toString());
				return arg12;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public Object[] getStatusValues(long id) {
		String sql = "select b.updateTime,b.enabled,b.examined from com.minierp.model.LoanBill b where b.id="
				+ id;
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 ? (Object[]) list.get(0) : null;
	}

	public BigDecimal getSmsCost(Date startDate, Date endDate, String billType)
			throws Exception {
		String sql = "select sum(b.cost) from com.minierp.model.LoanBill b where b.enabled=\'Y\' and b.examined=\'Y\' and b.billType=:billType and b.loanDate>=:startDate and b.loanDate<:endDate";
		Session sess = null;

		BigDecimal arg8;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("billType", billType);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			List list = q.list();
			if (!list.isEmpty() && list.get(0) != null) {
				arg8 = new BigDecimal(list.get(0).toString());
				return arg8;
			}

			arg8 = new BigDecimal(0);
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg8;
	}

	private String getCountSql(String type, Date startDate, Date endDate,
			String rpName, String mngName, long tsId) {
		String str = " and b.bill_type=:type";
		if (startDate != null) {
			str = str + " and b.loan_date>=:startDate";
		}

		if (endDate != null) {
			str = str + " and b.loan_date<:endDate";
		}

		if (rpName != null && !rpName.equals("")) {
			str = str + " and (b.rp_name like :rpName)";
		}

		if (mngName != null && !mngName.equals("")) {
			str = str + " and (b.manager_name like :managerName)";
		}

		str = str + " and b.tally_set_id=" + tsId;
		String sql = "select b.rp_name,sum(b.cost) as rem_cost from loan_bill b where b.enabled=\'Y\' and b.examined=\'Y\' "
				+ str + " group by b.rp_name";
		return sql;
	}

	private void setCountValue(SQLQuery q, String type, Date startDate,
			Date endDate, String rpName, String mngName) {
		if (startDate != null) {
			q.setDate("startDate", startDate);
		}

		if (endDate != null) {
			q.setDate("endDate", endDate);
		}

		if (rpName != null && !rpName.equals("")) {
			q.setString("rpName", "%" + rpName + "%");
		}

		if (mngName != null && !mngName.equals("")) {
			q.setString("managerName", "%" + mngName + "%");
		}

		q.setString("type", type);
	}

	public List getCountList(String type, Date startDate, Date endDate,
			String rpName, String mngName, long tsId, int startIndex, int num)
			throws Exception {
		if (mngName != null) {
			mngName = mngName.trim();
		}

		String sql = "select a.* from("
				+ this.getCountSql(type, startDate, endDate, rpName, mngName,
						tsId) + ") a" + " where rem_cost!=0"
				+ " order by rem_cost desc";
		Session sess = null;

		List arg13;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			this.setCountValue(q, type, startDate, endDate, rpName, mngName);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg13 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg13;
	}

	public List getCountTotal(String type, Date startDate, Date endDate,
			String rpName, String mngName, long tsId) throws Exception {
		if (mngName != null) {
			mngName = mngName.trim();
		}

		String sql = "select count(*),sum(rem_cost) from("
				+ this.getCountSql(type, startDate, endDate, rpName, mngName,
						tsId) + ") a" + " where rem_cost!=0";
		Session sess = null;

		List arg11;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			this.setCountValue(q, type, startDate, endDate, rpName, mngName);
			arg11 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg11;
	}

	public List getExamSmsCount(long[] tsIdArr) {
		if (tsIdArr != null && tsIdArr.length != 0) {
			String sql = "select b.billType,b.billCode from  com.minierp.model.LoanBill b where b.examSms=\'Y\'"
					+ PageHelper.createIdArrStr(tsIdArr, "b.tallySetId", true);
			return this.getHibernateTemplate().find(sql);
		} else {
			return new ArrayList();
		}
	}

	public List getExamSmsCount() {
		String sql = "select b.billType,b.billCode from  com.minierp.model.LoanBill b where b.examSms=\'Y\'";
		return this.getHibernateTemplate().find(sql);
	}

	public int updateExamSms() {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.LoanBill set examSms=\'N\' ,updateTime=:updateTime where examSms=\'Y\'";
						Query q = session.createQuery(hql);
						q.setTimestamp("updateTime", new Date());
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}
}